<?php

class ViewController extends Controller {

  public $content;

  public function init() {

    if (user()->getUserProperty('username') == ''){
      $this->redirect(FRONT_SITE_URL);
    }
  }

  public function actionIndex() {
    $this->layout = 'main_after_login';
    $this->content['addCss'] = array('homepage.css');
    Yii::app()->clientScript->registerScriptFile(WWW_URL . "js/js1/peoplemedia_002.js", CClientScript::POS_BEGIN);
    Yii::app()->clientScript->registerScriptFile(WWW_URL . "js/js1/index.js", CClientScript::POS_BEGIN);
    Yii::app()->clientScript->registerScriptFile(WWW_URL . "js/js1/iminterestedslideshow.js", CClientScript::POS_BEGIN);
    Yii::app()->clientScript->registerScriptFile(WWW_URL . "js/js1/fastactionmodule.js", CClientScript::POS_BEGIN);
    Yii::app()->clientScript->registerScriptFile(WWW_URL . "js/js1/newmembersinarea.js", CClientScript::POS_BEGIN);    
    $this->content['widget'] = array('application.widgets.newsletter.WInterest');
    $this->content['param']['widget'] = array(array('title' => 'abc'));

    $mProfile = new mProfile();
    $profile = $mProfile->getProfileWithPhotosPrimary(user()->getUserProperty('user_id'));
    $seeking_search = CONSTANT::getSeeking2Array();
    
    $data = array();
    $data['recommend_user'] = $mProfile->getProfileRecommend($seeking_search[$profile['seeking']]);
    $data['who_view_me'] = mUserView::model()->getUserViewed($profile['user_id']);
    
    $data['reminders'] = mUserFavorite::model()->getUserFavorited($profile['user_id']);
    $data['percent'] = $this->getPercentUserAnswer();
    $mGroupAnswerPerQues = new mGroupAnswerPerQues();
    $dataUserAnswer = $mGroupAnswerPerQues->getUserAnswer(user()->getUserProperty('user_id'));
    if($dataUserAnswer != false && $profile['gender']=='female'){
      Yii::app()->clientScript->registerScriptFile(WWW_URL . "js/js1/miniessayquestions.js", CClientScript::POS_BEGIN);
      $data['useranswer'] = $dataUserAnswer;
    }
    
    if ($profile['gender'] == 'male') {
      $this->render('index_women', array(
          'profile' => $profile,
          'data' => $data
      ));
    } else {
      $this->render('index_women', array(
          'profile' => $profile,
          'data' => $data
      ));
    }
  }

  /*
   * Get Percent User Answer
   */
  public function getPercentUserAnswer() {
    $mProfile = new mProfile();
    $mProfile = $mProfile->getProfileByUserId(user()->getUserProperty('user_id'));
    $about = 0;
    $greetings = 0;
    $personality = 0;
    $interests = 0;
    $global = 0;
    if($mProfile!=false){
      if($mProfile['looking']!=null) $about += 6.25;
      if($mProfile['occupation']!=null) $about += 6.25;
      if($mProfile['education']!=null) $about += 6.25;
      if($mProfile['income']!=null) $about += 6.25;
      if($mProfile['religion']!=null) $about += 6.25;
      if($mProfile['political_affiliation']!=null) $about += 6.25;
      if($mProfile['where_i_grew_up']!='') $about += 6.25;
      if($mProfile['tall']!=null) $about += 6.25;
      if($mProfile['body_type']!=null) $about += 6.25;
      if($mProfile['hair_color']!=null) $about += 6.25;
      if($mProfile['eyes_color']!=null) $about += 6.25;
      if($mProfile['ethnicity']!=null) $about += 6.25;
      if($mProfile['marial_status']!=null) $about += 6.25;
      if($mProfile['children']!=null) $about += 6.25;
      if($mProfile['pet']!='') $about += 6.25;
      if($mProfile['smoke']!=null) $about += 6.25;
      if($mProfile['greeting1']!=null) $greetings += 33.33;
      if($mProfile['greeting2']!=null) $greetings += 33.33;
      if($mProfile['greeting3']!=null) $greetings += 33.33;
      if($greetings>99) $greetings = 100;
      
      $mAllQuestion = mPersonalityQuestions::model()->findAll('sex=:sex and active=1', array(':sex'=>$mProfile['gender']));
      $mQuesUserAns = mPersonalityQuestions::model()->getQuesUserAns($mProfile['gender'],$mProfile['user_id']);      
      if(count($mAllQuestion) == count($mQuesUserAns)){
        $personality = 100;
      }else{
        $percentOneQuestion = 100/count($mAllQuestion);
        $personality = round($percentOneQuestion * count($mQuesUserAns));
      }
      
      if($mProfile['interestImg1']!=null && $mProfile['interestImg1'] != 0) $interests += 33.33;
      if($mProfile['interestImg2']!=null && $mProfile['interestImg2'] != 0) $interests += 33.33;
      if($mProfile['interestImg3']!=null && $mProfile['interestImg3'] != 0) $interests += 33.33;
      if($interests>99) $interests = 100;
      
    }
    $global = ($about + $greetings + $personality + $interests)/4;
    
    $data['about'] = round($about);
    $data['greetings'] = round($greetings);
    $data['personality'] = round($personality);
    $data['interests'] = round($interests);
    $data['global'] = round($global);
    return $data;
  }
}